package com.longteng.diamond.service;


import com.google.common.collect.Lists;
import com.longteng.diamond.dao.InterfaceTestMapper;
import com.longteng.diamond.domain.InterfaceTest;

import com.longteng.diamond.po.TablePage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
/**
 * Created by Administrator
 * DATE : 2018/6/28
 * TIME : 20:33
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Slf4j
@Component
public class InterfaceTestService {
    @Autowired
    InterfaceTestMapper interfaceTestMapper;
    /**
     * 获取接口
     * @param interfaceTest
     * @return interfaceTest
     * */
    public TablePage getInterfaceTest(InterfaceTest interfaceTest){
        TablePage tablePage=new TablePage();
        try {
            /**通过limit计算返回的数据**/
            List<InterfaceTest> interfaceTestList = interfaceTestMapper.getInterfaceTest(interfaceTest);
            tablePage.setRows(interfaceTestList);
            /**获取当前where条件的所有行数，如果你的where条件不变化的话，每次返回的所有行数都是一样的**/
            long total = interfaceTestMapper.queryAllRow(interfaceTest);
            tablePage.setTotal(total);
            /**interfaceTestList 和total 它们的where条件应该是一模一样的**/
        }catch (Exception e){
            log.error("查询接口列表异常",e);
        }
        return tablePage;
    }
    /**
     *  新增接口
     * @param interfaceTest
     * @return
     * */
    public void addInterfaceTest(InterfaceTest interfaceTest) {
        interfaceTestMapper.addInterfaceTest(interfaceTest);
    }
    /**
     *  修改接口
     * @param interfaceTest
     * @return void
     * */
    public void updateInterfaceTest(InterfaceTest interfaceTest) {
        interfaceTestMapper.updateInterfaceTest(interfaceTest);
    }

    /**
     * 根据id获取接口
     * @param interfaceId
     * @return interfaceTest
     */
    public InterfaceTest findInterfaceTestById(Integer interfaceId){
       return interfaceTestMapper.findInterfaceTestById(interfaceId);
    }
    /**
     * 删除接口
     * @param interfaceId
     * @return void
     */
    public void deleteInterfaceTestById(Integer interfaceId) {
        interfaceTestMapper.deleteInterfaceTestById(interfaceId);
    }
    /**
     * 批量绑定接口header
     * @param headers
     * @param interfaceIds
     * @return
     */
    public void bindHeaders(String headers, Integer[] interfaceIds){
        List<InterfaceTest> interfaceTestList= Lists.newArrayList();
        for(Integer id:interfaceIds){
            InterfaceTest interfaceTest=new InterfaceTest();
            interfaceTest.setInterfaceId(id);
            interfaceTest.setHeaders(headers);
            interfaceTestList.add(interfaceTest);
        }
        interfaceTestMapper.bindHeaders(interfaceTestList);
    }
    /**
     * 批量绑定接口header
     * @param interfaceIds
     * @return
     */
    public List<InterfaceTest> findInterfaceTestByIds(List interfaceIds){
       return interfaceTestMapper.findInterfaceTestByIds(interfaceIds);
    }
    /**
     * 获取接口用例
     * @param projectId
     * @return List<InterfaceTest>
     */
    public List<InterfaceTest> findInterfaceAndTestCase(Integer projectId){
       return interfaceTestMapper.findInterfaceAndTestCase(projectId);
    }

}
